import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.css'

import {postRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";
import {initMenu} from "./utils/menus";
import {downloadRequest} from "./utils/download";

//以插件得方式使用请求
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.downloadRequest = downloadRequest

Vue.config.productionTip = false

Vue.use(ElementUI)
//前置路由
router.beforeEach((to,from,next)=>{
  //判断是否登录
  if(window.sessionStorage.getItem('tokenStr')){
    //初始化菜单
    initMenu(router,store)
    next()
    //获取用户信息
    if(!window.sessionStorage.getItem('user')){
      return getRequest('/admin/info').then(res=>{
        if(res){
          console.log(res);
          //保存用户信息在本地缓存
          window.sessionStorage.setItem('user',JSON.stringify(res))
          next()
        }
      })
    }
  }else {
    //没有登录的情况
    if(to.path === '/'){
      //1.想要去登录页直接跳转
      next()
    }else {
      //2.要去之前退出来的页面   重定向
      next('/?redirect='+to.path)
    }
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
